/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package deatax.state;

import deatax.GameData;
import deatax.Highlight;
import deatax.PathInfo;
import deatax.Tile;
import deatax.network.Client;
import deatax.network.Messages;
import deatax.network.NetworkListener;
import deatax.unit.UnitStack;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

/**
 *
 * @author pson
 */
public class UnitMoveState extends GameState implements NetworkListener {

    UnitMoveMouseListener listener;
    private UnitStack stack;
    private int num;
    ArrayList<PathInfo> valid;

    public UnitMoveState(GameData data, UnitStack stack, int num) {
        super("Unit Move", data);
        this.stack = stack;
        this.num = num;

        valid = data.getMap().getRange(stack.getTile(), stack.getType().getMovement(), true, stack.getType().hasSkill("sail"), stack.getOwner());//, null, null, null, null, true, null, null, true);

        data.getMap().clearHighlights();
        Highlight highlight = new Highlight();
        int[] area = new int[valid.size() * 2];
        int i = 0;
        for (PathInfo move : valid) {
            Tile t = move.getTile();
            area[i] = t.getX();
            area[i+1] = t.getY();
            i += 2;
        }
        highlight.tiles = area;
        highlight.colour = new Color(192,128,255);
        data.getMap().addHighlight(highlight);
    }

    @Override
    public void begin() {

        listener = new UnitMoveMouseListener();
        data.getDisplay().addMouseListener(listener);
    }

    @Override
    public void finish() {
        //System.out.println("exiting");
        data.getMap().clearHighlights();
        data.getState().popState();
    }

    @Override
    public void die() {
        if (listener != null) {
            data.getDisplay().removeMouseListener(listener);
        }
    }

    @Override
    public void processMessage(int type, String[] arg) {
        commonMessages(type, arg);
    }

    protected class UnitMoveMouseListener extends MouseAdapter {

        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON1) {
                Tile t = data.getRender().getHover();
                if (t != null && t != stack.getTile()) {
                    boolean hasTile = false;
                    for (PathInfo p : valid) {
                        if (p.getTile() == t) {
                            hasTile = true;
                            break;
                        }
                    }
                    if (hasTile) {
                        Client.get().sendMessage(Messages.UNIT, new String[]{stack.getOwner().getName(), 
                            stack.getType().getName(), String.valueOf(-num), String.valueOf(stack.getTile().getX()), String.valueOf(stack.getTile().getY())});
                        Client.get().sendMessage(Messages.UNIT, new String[]{stack.getOwner().getName(), 
                            stack.getType().getName(), String.valueOf(num), String.valueOf(t.getX()), String.valueOf(t.getY()), "true"});
                        Client.get().sendMessage(Messages.LOG, new String[]{stack.getOwner().getName(), "Moved " + num + " " + stack.getType().getName() + (num != 1 ? "s" : "")});
                        finish();
                    }
                }
            } else if (e.getButton() == MouseEvent.BUTTON3) {
                finish();
            }
        }
    }
}
